<template>
  <div class="banner-container">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <span>加载中...</span>
    </div>

    <!-- 错误状态 -->
    <div v-else-if="error" class="error-container">
      <span class="error-icon">⚠️</span>
      <p>轮播图加载失败</p>
      <el-button type="primary" @click="fetchBanners">重新加载</el-button>
    </div>

    <!-- 正常状态 -->
    <el-carousel
        v-else
        :height="carouselHeight"
        :interval="5000"
        arrow="never"
        :pause-on-hover="true"
        @change="handleCarouselChange"
    >
      <el-carousel-item v-for="item in banners" :key="item.id">
        <img
            :src="item.image"
            :alt="item.title"
            class="banner-image"
            @load="handleImageLoad"
        />
        <div class="banner-content">
          <h2>{{ item.title }}</h2>
          <p v-show="!isMobile || showDescription">{{ item.description }}</p>
<!--          <el-button-->
<!--              type="primary"-->
<!--              size="large"-->
<!--              class="learn-more-btn"-->
<!--              @click="() => handleLearnMore(item)"-->
<!--          >-->
<!--            了解更多-->
<!--          </el-button>-->
        </div>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { getBannerInfo } from '@/api/banner' // 根据实际路径调整

// 响应式状态
const banners = ref([])
const loading = ref(true)
const error = ref(false)
const carouselHeight = ref('600px')
const isMobile = ref(false)
const showDescription = ref(false)

// 获取轮播图数据
const fetchBanners = async () => {
  try {
    loading.value = true
    error.value = false
    const response = await getBannerInfo()
    // 根据实际接口返回结构调整
    // 假设接口返回数据格式为 { data: [...] } 或直接返回数组
    if (Array.isArray(response.data)) {
      banners.value = response.data
    } else if (Array.isArray(response.data.data)) {
      banners.value = response.data.data
    } else {
      console.warn('接口返回数据格式不符合预期:', response)
      banners.value = []
    }

    // 如果没有数据，显示错误状态
    if (banners.value.length === 0) {
      error.value = true
    }
  } catch (err) {
    console.error('获取轮播图数据失败:', err)
    error.value = true
    banners.value = [] // 确保banners是数组
  } finally {
    loading.value = false
  }
}

// 处理轮播图切换
const handleCarouselChange = (currentIndex) => {
  if (isMobile.value) {
    showDescription.value = true
    setTimeout(() => {
      showDescription.value = false
    }, 3000)
  }
}

// 图片加载完成后调整布局
const handleImageLoad = () => {
  if (isMobile.value) {
    adjustMobileLayout()
  }
}

// 调整移动端布局
const adjustMobileLayout = () => {
  const imgElement = document.querySelector('.banner-image')
  if (imgElement) {
    const aspectRatio = imgElement.naturalHeight / imgElement.naturalWidth
    const viewportWidth = window.innerWidth
    carouselHeight.value = `${viewportWidth * aspectRatio}px`
  }
}

// 处理了解更多点击
const handleLearnMore = (item) => {
  console.log('了解更多被点击:', item)
  // 这里可以根据item中的链接信息进行跳转
  // 例如: window.location.href = item.linkUrl
}

// 响应式处理
const handleResize = () => {
  const width = window.innerWidth
  isMobile.value = width < 768

  if (width < 576) {
    carouselHeight.value = '300px'
  } else if (width < 768) {
    carouselHeight.value = '400px'
  } else if (width < 992) {
    carouselHeight.value = '500px'
  } else {
    carouselHeight.value = '600px'
  }

  if (isMobile.value) {
    adjustMobileLayout()
  }
}

// 生命周期钩子
onMounted(() => {
  handleResize()
  window.addEventListener('resize', handleResize)
  fetchBanners() // 组件挂载时获取数据
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
/* 原有样式保持不变，增加加载和错误状态样式 */
.loading-container, .error-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 600px;
  background-color: #f5f5f5;
  color: #999;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #409EFF;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

.error-icon {
  font-size: 40px;
  margin-bottom: 16px;
}

.error-container p {
  margin-bottom: 16px;
  color: #666;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 原有样式 */
.banner-container {
  position: relative;
  width: 100%;
  margin-top: -80px;
  z-index: 10;
  transform: translateZ(0);
  backface-visibility: hidden;
  perspective: 1000px;
  will-change: transform;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  transform: translateZ(0);
  backface-visibility: hidden;
}

.banner-content {
  position: absolute;
  bottom: 100px;
  left: 100px;
  color: white;
  text-align: left;
  max-width: 500px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  transform: translateZ(0);
  backface-visibility: hidden;
  will-change: transform;
}

.banner-content h2 {
  font-size: 36px;
  margin-bottom: 20px;
  transform: translateZ(0);
}

.banner-content p {
  font-size: 18px;
  margin-bottom: 30px;
  transition: opacity 0.3s ease;
  transform: translateZ(0);
  backface-visibility: hidden;
}

.learn-more-btn {
  background-color: transparent !important;
  border: 2px solid white !important;
  color: white !important;
  border-radius: 30px !important;
  padding: 12px 32px !important;
  font-weight: 500 !important;
  letter-spacing: 1px;
  transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1) !important;
  transform: translateZ(0);
  backface-visibility: hidden;
  position: relative;
  overflow: hidden;
  will-change: transform;
}

.learn-more-btn::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: white;
  z-index: -1;
  transform: scaleX(0);
  transform-origin: right;
  transition: transform 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.learn-more-btn:hover {
  color: #333 !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px) translateZ(0);
}

.learn-more-btn:hover::before {
  transform: scaleX(1);
  transform-origin: left;
}

.learn-more-btn:active {
  transform: translateY(0) translateZ(0);
  transition: all 0.1s ease;
}

.learn-more-btn:focus {
  outline: none;
}

/* 响应式样式 */
@media (max-width: 1199px) {
  .banner-content {
    max-width: 450px;
  }
}

@media (max-width: 991px) {
  .banner-content {
    left: 60px;
    bottom: 80px;
    max-width: 400px;
  }

  .banner-content h2 {
    font-size: 28px;
  }

  .banner-content p {
    font-size: 16px;
  }
}

@media (max-width: 767px) {
  .banner-content {
    left: 30px;
    bottom: 60px;
    max-width: 300px;
  }

  .banner-content h2 {
    font-size: 24px;
    margin-bottom: 15px;
  }

  .banner-content p {
    font-size: 14px;
    margin-bottom: 20px;
    opacity: 0;
  }

  .learn-more-btn {
    padding: 10px 24px !important;
    font-size: 14px !important;
  }

  .el-carousel__arrow {
    transform: scale(0.8);
  }
}

@media (max-width: 575px) {
  .banner-content {
    left: 20px;
    bottom: 40px;
    max-width: 80%;
  }

  .banner-content h2 {
    font-size: 20px;
  }

  .banner-content p.show {
    opacity: 1;
  }

  @supports (-webkit-touch-callout: none) {
    .banner-container {
      touch-action: pan-y;
    }
  }
}

/* 覆盖Element Plus默认样式 */
:deep(.el-carousel__container) {
  position: relative;
  transform: translateZ(0);
  backface-visibility: hidden;
  perspective: 1000px;
}

:deep(.el-carousel__arrow) {
  background-color: rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
  transform: translateZ(0);
}

:deep(.el-carousel__arrow:hover) {
  background-color: rgba(255, 255, 255, 0.5);
}

:deep(.el-carousel__item) {
  transform: translateZ(0);
  backface-visibility: hidden;
  will-change: transform;
}
</style>
